using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Nivel21.XEngine.Rendering.Interfaces;

namespace Nivel21.XEngine.Rendering.Objects
{
    /// <summary>
    /// This class represent a 3D model that can be drawn
    /// </summary>
    public class NormalMappedMesh : Entity, ILoadable, IRenderable
    {
        private string modelAsset = "";

        #region "Constructors"

        public NormalMappedMesh(string ModelAsset)
        {
            this.modelAsset = ModelAsset;
        }

        #endregion

        #region ILoadable Members

        /// <summary>
        /// Load the mesh contents: model and shader
        /// </summary>
        public void LoadGraphicsContent(GraphicsDevice myDevice, Microsoft.Xna.Framework.Content.ContentManager myLoader)
        {
            //load the mesh
            this.meshModel = myLoader.Load<Model>(modelAsset);
        }

        #endregion

        #region IRenderable Members

        public void Render(ref Matrix worldMatrix, GraphicsDevice myDevice)
        {

            foreach (ModelMesh mesh in this.meshModel.Meshes)
            {

                foreach (Effect effect in mesh.Effects)
                {
                    //Aply the engine parameters
                    Engine.Instance.ShaderManager.AplyEngineShaderParameters(worldMatrix, effect);

                    //Aply custom shader parameters
                    if (effect.Parameters["SpecularPower"] != null)
                        effect.Parameters["SpecularPower"].SetValue(1.0f);

                    if (effect.Parameters["Shininess"] != null)
                        effect.Parameters["Shininess"].SetValue(1.0f);
                }

                mesh.Draw();
            }

        }

        #endregion

    }

}


